package cn.com.util;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.api.service.SystemService;
import cn.com.util.*;

public class UserLogin {

	public static boolean isUserLogin(HttpServletRequest request, HttpServletResponse response,
			SystemService systemService) throws Exception {
		boolean flag = true;
		String userid = request.getParameter("userid");
		String token = request.getParameter("token");
		boolean result = NullJudge.judgeSZ(new String[] { userid, token });
		if (!result) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put("datas", "参数不正确");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(JsonUtil.object2json(map));
			out.close();
			flag = false;
		}
		if (!systemService.isUserLogin(userid, token)) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put("datas", "用户的token值不正确");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(JsonUtil.object2json(map));
			out.close();
			flag = false;
		}
		return flag;
	}

	public static boolean isMemberLogin(HttpServletRequest request, HttpServletResponse response,
			SystemService systemService) throws Exception {
		boolean flag = true;
		String userid = request.getParameter("userid");
		String token = request.getParameter("token");
		boolean result = NullJudge.judgeSZ(new String[] { userid, token });
		if (!result) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put("datas", "参数不正确");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(JsonUtil.object2json(map));
			out.close();
			flag = false;
		}
		if (!systemService.isMemberLogin(userid, token)) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put("datas", "用户的token值不正确");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(JsonUtil.object2json(map));
			out.close();
			flag = false;
		}
		return flag;
	}

}
